/**
 * 
 */
package falstad;


import java.io.File;
//Possibly delete these two


/**
 * This class is a wrapper class to startup the Maze as a Java application
 * 
 *
 * This code is refactored code from Maze.java by Paul Falstad, www.falstad.com, Copyright (C) 1998, all rights reserved
 * Paul Falstad granted permission to modify and use code for teaching purposes.
 * Refactored by Peter Kemper
 */
//public class MazeApplication extends JFrame {

public class MazeApplication{

	Maze maze ;
	GUIManager gui;
	
	/**
	 * Constructor
	 */
	public MazeApplication() {
		super() ;
		System.out.println("MazeApplication: maze will be generated with a randomized algorithm.");
		maze = new Maze() ;
		init() ;
	}

	/**
	 * Constructor that loads a maze from a given file or uses a particular method to generate a maze
	 */
	public MazeApplication(String parameter) {
		super() ;
		if ("Prim".equalsIgnoreCase(parameter))
		{
			System.out.println("MazeApplication: generating random maze with Prim's algorithm");
			maze = new Maze(1) ;
			init() ;
		}
		else if ("Eller".equalsIgnoreCase(parameter))
		{
			System.out.println("MazeApplication: generating random maze with Eller's algorithm");
			maze = new Maze(2) ;
			init() ;
		}
		else
		{
			if (readFile(parameter))
			{
				
			}
			else
			{
				System.out.println("MazeApplication: unknown parameter value: " + parameter + " ignored, operating in default mode.");
				maze = new Maze() ;
				init() ;
			}
		}
		
		

	}
	
	/**
	 * Initializes some internals and puts the game on display.
	 */
	private void init() {
		GUIManager gui = new GUIManager(maze);
		maze.init();	
	//	startRobot();
	}
	
	

	public boolean readFile(String parameter)
	{
		File f = new File(parameter) ;
		if (f.exists() && f.canRead())
		{
			System.out.println("MazeApplication: loading maze from file: " + parameter);
			maze = new Maze() ;
			init();
			MazeFileReader mfr = new MazeFileReader(parameter) ;
			maze.mazeh = mfr.getHeight() ;
			maze.mazew = mfr.getWidth() ;
			Distance d = new Distance(mfr.getDistances()) ;
			maze.newMaze(mfr.getRootNode(),mfr.getCells(),d,mfr.getStartX(), mfr.getStartY()) ;
		}
		else
		{
			return false;
		}
		return true;
	}


	
	/**
	 * Main method to launch Maze as a java application.
	 * The application can be operated in two ways. The intended normal operation is to provide no parameters
	 * and the maze will be generated by a particular algorithm. If a filename is given, the maze will be loaded
	 * from that file. The latter option is useful for development to check particular mazes.
	 * @param args is optional, first parameter is a filename with a given maze
	 */
	public static void main(String[] args) {
		MazeApplication a ; 
		switch (args.length) {
		case 1 : a = new MazeApplication(args[0]);
		break ;
		case 0 : 
		default : a = new MazeApplication() ;
		break ;
		}
		//a.gui.repaint() ;
	}

}
